package com.xiaoding.serialhelper;


import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.carbs.android.avatarimageview.library.AvatarImageView;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2017/5/21 0021.
 */

public class StringAdapter extends BaseAdapter {
    private Context context;
    private List<String> mAppList;
    private  int font_count = 0;
    private final  int ItemMaxLength = 15;

    public StringAdapter(Context context, List<String> mAppList) {
        this.context = context;
        this.mAppList = mAppList;
        font_count = contentAdapter();
    }

    @Override
    public int getCount() {
        return mAppList.size();
    }

    @Override
    public String getItem(int position) {
        return mAppList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private int contentAdapter()
    {
        int count = ItemMaxLength;
        Paint pFont = new Paint();
        Rect rect = new Rect();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        pFont.getTextBounds("豆", 0, 1, rect);
        Log.v("StringAdapter", "font height:"+rect.height()+"width:"+rect.width());
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        Log.v("StringAdapter", "screen height:"+ height +"width:"+ width);
        count = width/(rect.width() * 2);
        Log.v("StringAdapter", "font count is:" + count);
        return  count;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_list_app, null);
            new StringAdapter.ViewHolder(convertView);
        }
//        AppAdapter.ViewHolder holder = (AppAdapter.ViewHolder) convertView.getTag();
        StringAdapter.ViewHolder holder = (StringAdapter.ViewHolder) convertView.getTag();
        String item = getItem(position);
  //      holder.iv_icon.setImageDrawable(item.loadIcon(packageManager));
        if(item.length() > font_count) {
            holder.tv_name.setText(item.substring( 0 , font_count));
        }else
        {
            holder.tv_name.setText(item);
        }
        return convertView;
    }

    class ViewHolder {
    //    AvatarImageView iv_icon;
        TextView tv_name;
        public ViewHolder(View view) {
      //      iv_icon = (AvatarImageView ) view.findViewById(R.id.iv_icon);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
        //    iv_icon.setTextAndColor("HEX", AvatarImageView.COLORS[0]);//直接设置颜色
            view.setTag(this);

        }
    }


}
